//
//  iOSCodeLearningTests.m
//  iOSCodeLearningTests
//
//  Created by Ace on 2017/10/31.
//  Copyright © 2017年 Ace. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "UITestViewController.h"
#import "NetWorkAssistant.h"

@interface UITestViewController(iOSCodeLearningTests)
- (NSString *)privateFuc;
@property (nonatomic, copy) NSString *privateString;
@end

@interface iOSCodeLearningTests : XCTestCase

@property (nonatomic, strong) UITestViewController *vc;

@end

@implementation iOSCodeLearningTests
//每个test方法执行之前调用，在此方法中可以定义一些全局属性，类似controller中的viewdidload方法
- (void)setUp {
    [super setUp];
    self.vc = [[UITestViewController alloc]init];
}
//每个test方法执行之后调用,释放测试用例的资源代码，这个方法会每个测试用例执行后调用

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testExample {
    XCTAssertNotNil(self.vc.view,@"界面未能加载出来");
}


-(void)testFuc{
    NSInteger result = [self.vc getNumWith:2 andB:2];
    XCTAssertEqual(result, 4 ,@"getNumWith: andB: 测试失败");
}

-(void)testImage{
    UIImage *image = [UIImage imageNamed:@"111"];
    [self measureBlock:^{
        UIImage *resizeImage = [self.vc imageWithImage:image scaledToSize:CGSizeMake(100, 100)];
        XCTAssertNotNil(image,@"没有返回处理后的图片");
        CGFloat resizeWidth = resizeImage.size.width;
        CGFloat resizeHeight = resizeImage.size.height;
        XCTAssertTrue((resizeWidth == 100 && resizeHeight == 100),@"处理后的图片尺寸不对");
    }];
    
}
- (void)testNetWork{
    // 1 初始化 expectation
    XCTestExpectation *exp = [self expectationWithDescription:@"测试出错了"];
    
    // 2 调接口
    [[NetWorkAssistant shareNetWorkAssistant] setRequestHeaderWithDict:@{@"TOKEN" :@"Timeface-iOS",
                                                                         @"USERID" :@"718385313074",
                                                                         @"DEVICEID" :[[[UIDevice currentDevice] identifierForVendor] UUIDString].mutableCopy
                                                                         }];
    
    [[NetWorkAssistant shareNetWorkAssistant] get:@"http://stg2.v5time.net/tfmobile/crowdfunding/index"
                                       parameters:@{@"isGzip" : @"0"}
                                         progress:nil
                                          success:^(id resposeObject) {
                                              //3 XCTestExpectation条件已满足，接下来的测试代码可以开始执行了。
                                              [exp fulfill];
                                              
                                              XCTAssertTrue(resposeObject[@"status"],@"接口失败");
                                              
                                              NSLog(@"response :%@",resposeObject);
                                          }
                                          failure:^(NSError * _Nonnull error) {
                                              
                                              [exp fulfill];
                                              NSLog(@"error : %@",error);
                                          }];
    
    [self waitForExpectationsWithTimeout:60 handler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"Timeout Error: %@", error);
        }
    }];

}
//异步测试，使用expectationForPredicate,设置一个期望，在规定时间内满足期望则测试通过
- (void)testAsynExampleWithExpectationForPredicate {
    
    UIImageView *imageView = [[UIImageView alloc]init];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        imageView.image = [UIImage imageNamed:@"111"];
    });
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"image != nil"];
    
   
    
    [self expectationForPredicate:predicate evaluatedWithObject:imageView handler:nil];
    
    // 超时后执行
    [self waitForExpectationsWithTimeout:2.0 handler:^(NSError * _Nullable error) {
        NSLog(@"predicate 评估超时了");
    }];
}
//异步测试，使用expectationForNotification,该方法监听一个通知,如果在规定时间内正确收到通知则测试通过
- (void)testAsynExampleWithExpectationForNotification {
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNoti" object:nil];
    });
    
    [self expectationForNotification:@"TestNoti" object:nil handler:^BOOL(NSNotification * _Nonnull notification) {
       
        NSLog(@"在规定的时间内监听到通知---“TestNoti”");
        return YES;
    }];
    
    //，如果没有满足测试条件就报错
    [self waitForExpectationsWithTimeout:2.0 handler:^(NSError * _Nullable error) {
       NSLog(@"在规定的时间内没有监听到通知---“TestNoti”");
    }];

}


-(void)testPrivate{
    // 测试私有方法啊
    XCTAssertEqualObjects(self.vc.privateFuc, @"1111111",@"私有方法返回失败");
    XCTAssertEqualObjects(self.vc.privateString, @"私有属性",@"");
}


@end
